﻿using AntdUI;
using System.Data;
using Wen.Core.Extensions;
using WenAntdUI.Extensions;
using WenAntdUI.Services;

namespace WenAntdUI.Pages.SysAdmin
{
    [Description("角色管理")]
    public partial class SysRolePage : UserControl
    {
        private readonly IFreeSql fsql;
        private readonly MenuService menuService;
        public SysRolePage(IFreeSql fsql, MenuService menuService)
        {
            this.fsql = fsql;
            this.menuService = menuService;
            InitializeComponent();

            this.adminTable1.SetColumns<SysRole>();
            this.adminTable1.Query();

            InitMenuItem();

            this.adminTable1.CellClick += AdminTable1_CellClick;
        }

        private SysRole SelectedSysRole { get; set; }

        private void AdminTable1_CellClick(object sender, TableClickEventArgs e)
        {
            if (e.Record is not SysRole item)
                return;
            SelectedSysRole = item;

            var menus = fsql.Select<SysRoleMenu>().Where(a => a.RoleName == item.RoleName).ToList();

            tree1.SetCheckeds(false);
            setCheckeds(tree1.Items);
            //设置选中项
            void setCheckeds(TreeItemCollection treeItems)
            {
                foreach (var t in treeItems)
                {
                    if (t.Sub.Count > 0)
                    {
                        setCheckeds(t.Sub);
                    }
                    if (menus.Exists(a => a.MenuCode == t.Name))
                    {
                        t.Checked = true;
                    }
                }
            }
        }

        /// <summary>
        /// 初始化 菜单项
        /// </summary>
        private void InitMenuItem()
        {
            var menus = menuService.GetSysMenus();
            this.tree1.Items.Clear();
            this.tree1.Items.AddRange(getTreeItem(menus));
            this.tree1.ExpandAll();

            List<TreeItem> getTreeItem(List<SysMenu> sysMenus)
            {
                var items = new List<TreeItem>();
                foreach (var menu in sysMenus)
                {
                    var item = new TreeItem()
                    {
                        IconSvg = menu.Icon,
                        Text = menu.Name,
                        Tag = menu,
                        Name = menu.Code,
                    };
                    if (menu.Child.Count > 0)
                    {
                        item.Sub.AddRange(getTreeItem(menu.Child));
                    }
                    items.Add(item);
                }
                return items;
            }
        }

        /// <summary>
        /// 按钮点击应用事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button1_Click(object sender, EventArgs e)
        {
            if (SelectedSysRole is null)
                return;

            var checks = tree1.GetCheckeds();

            var items = checks.Select(a => new SysRoleMenu() { MenuCode = a.Name, RoleName = SelectedSysRole.RoleName });

            fsql.Delete<SysRoleMenu>().Where(t => t.RoleName == SelectedSysRole.RoleName).ExecuteEffects();
            fsql.Insert<SysRoleMenu>().AppendData(items).ExecuteEffects();
        }
    }
}
